--[[
FilePath: \Lua\View\DungeonRandomEventUI\UIRandomEventShop.lua
主线随机事件--购买道具
--]]

require 'View/UIBase/UIBase'

UIRandomEventShop = UIBase.New(PanelResNames.UIRandomEventShop)
local _M = UIRandomEventShop

_M.hasInit = false;
_M.shopCfgItem = nil;
_M.shopItemList = {};
_M.showBuyTime = {};

function _M.Init(func)
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("Btn_BgClose"), self.OnBack);
        self.itemList = self:GetChild("content");
        self.textHelpTimes = self:GetChild("Text_Time");

        self.shopGoods = G.dataTable["t_shopgoods"];
        self.itemData = G.dataTable["t_item"];
        self.equipData = dataTable.getDataTable("t_equip");

        self.itemList.itemRenderer = self.setData;
        self.isShowReward = false;
        _M.hasInit = true

        self:addEvent();
    end
end

function _M:addEvent()
    self:RegisterEvent(Config.EventType.Fresh_DungeonRandomEventAccept,self.updateView);
end

function _M:removeEvent()
    self:UnRegisterEvent(Config.EventType.Fresh_DungeonRandomEventAccept,self.updateView);
end

-- 显示
function _M:OnEnable()
    if not self.hasInit then
        self:OnBack();
        return;
    end
    self:refresh();
end

function _M:refresh()
    self.data = logicMgr.DungeonRandomEventManager.getDungeonRandomEventData();
    if self.data == nil or self.shopGoods == nil or self.data.shop == nil or HelperFunc.GetServerTime() > self.data["overdueTime"] then
        self:OnBack();
        return;
    end

    --if self.shopCfgItem == nil then
    --    self.shopCfgItem = {};
    --    for k, v in pairs(self.data.shop) do
    --        local item = self.shopGoods[tostring(v.commodityId)];
    --        if item ~= nil then
    --            table.insert(self.shopCfgItem, item);
    --        end
    --    end
    --end
    --table.sort(self.shopCfgItem, function(a, b)
    --    return a.f_CommodityID < b.f_CommodityID;
    --end)
    self:updateView();

    self.randomEventTimeCount = self.data["overdueTime"] - HelperFunc.GetServerTime();
    self:dungeonRandomEventTimeCount();
    self.randomEventTimer = utimer.timeCall(self.dungeonRandomEventTimeCount, 1000, -1, self);
end

function _M:dungeonRandomEventTimeCount()
    self.randomEventTimeCount = self.randomEventTimeCount - 1;
    if self.randomEventTimeCount < 0 then
        self:removeRandomEventTimer();
        if not self.isShowReward then
            self:OnBack();
        end
        return;
    end
    self.textHelpTimes.text = timeToTimeFormat(self.randomEventTimeCount);
end

function _M:removeRandomEventTimer()
    if self.randomEventTimer then
        utimer.remove(self.randomEventTimer);
        self.randomEventTimer = nil;
    end
end

function _M:updateView(reward)
    self.isShowReward = false;
    self.data = logicMgr.DungeonRandomEventManager.getDungeonRandomEventData();
    if reward ~= nil then
        self.isShowReward = true;
        logicMgr.DungeonRandomEventManager.getFetchReward(reward, function()
            if self.data == nil or HelperFunc.GetServerTime() > self.data["overdueTime"] then
                self.isShowReward = false;
                self:OnBack();
            end
        end);
    end

    if self.data ~= nil and self.data.shop ~= nil then
        --self.showBuyTime ={};
        self.shopItemList = {};
        for k, v in pairs(self.data.shop) do
            --self.showBuyTime[tostring(v.commodityId)] = v;
        --end

        --for i = 1, #self.shopCfgItem do
            local item = {};
            local add = true;
            --local shopCfg = self.shopCfgItem[i];
            local shopCfg = self.shopGoods[tostring(v.commodityId)];
            item.eventShopCommodityId = v.commodityId;
            local itemModelId = v.itemModeId;
            item.itemModelId = tostring(itemModelId)
            --local showBuyTimeItem = self.showBuyTime[tostring(shopCfg.f_CommodityID)];
            if tonumber(shopCfg.f_Stock) < 0 then
                item["remainCount"] = 1;
            else
                item["remainCount"] = tonumber(shopCfg.f_Stock) - tonumber(v.buyTimes);
            end
            item["shopItemId"] = v.index;

            if logicMgr.ItemManager.IsEquip(item.itemModelId) then
                local e =  self.equipData[item.itemModelId];
                if e then
                    item["icon"] = e.f_EquipIcon;
                    item["quality"] = e.f_EquipQuality;
                    item["itemType"] = EnumConst.ItemTypeEnum.Equip;
                    item["name"] = getLanguage(e.f_EquipName);
                    item["desc"] = getLanguage(e.f_EquipDesc);
                else
                    logError("can not find equipconfig by id:" .. itemModelId)
                    add = false
                end
            else
                local it = self.itemData[item.itemModelId];
                if it then
                    item["icon"] = it.f_Icon;
                    item["quality"] = it.f_Quality;
                    item["itemType"] = it.f_BagType;
                    item["name"] = getLanguage(it.f_ItemName);
                    item["desc"] = getLanguage(it.f_Desc);
                else
                    logError("can not find itemconfig by id:" .. itemModelId)
                    add = false
                end
            end

            item["num"] =  shopCfg.f_Quantity;
            local price = decodeJsonStrToTabel(shopCfg.f_Price)[1];
            item["monyType"] = price[1];
            item["moneyIcon"] = self.itemData[tostring(price[1])].f_Icon;
            item["price"] = price[2];
            item["discount"] = shopCfg.f_Discount;
            item["specialBuyType"] = 1;
            if add then
                table.insert(self.shopItemList, item);
            end
        end
        self.itemList.numItems = #self.shopItemList;
    end
end

function _M.setData(index, obj)
    local itemValue = _M.shopItemList[index + 1];
    local item = CtrlManager.GetCtrl(PanelNames.ShopItem);
    if item ~= nil then
        item.initData(obj, itemValue);
    end
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    self:removeRandomEventTimer();
    self.hasInit = false;
    self.shopCfgItem = nil;
    self.shopItemList = {};
    self.showBuyTime ={};
end

return _M;